<?php

namespace {

require_once(\hpnWse\fGetWseDiry() . 'hpnWse/HttpSvc.php');
require_once(\hpnWse\fGetWseDiry() . 'hpnWse/SqlUtil.php');
require_once(\hpnWse\fGetWseDiry() . 'hpnWse/PgntCch.php');
require_once(\hpnWse\fGetWseDiry() . 'hpnWse/nWp/(0)Base.php');
require_once(\hpnWse\fGetWseDiry() . 'hpnWse/nMvc/Dmdl.php');

use \hpnWse\stNumUtil;
use \hpnWse\stStrUtil;
use \hpnWse\stObjUtil;
use \hpnWse\stAryUtil;
use \hpnWse\stHttpSvc;
use \hpnWse\stSqlUtil;
use \hpnWse\nWp\stBase as stWpBase;

//【数据库配置】
\hpnWse\stHttpSvc::$c_PdoCfg = array(
	'c_Dsn' => 'mysql:host=' . DB_HOST,
	'c_Usnm' => 'root',
	'c_Pswd' => DB_PASSWORD,
	'c_Optns' => null,
	'c_Salt' => '6045422896676840'
);

} // namesapce

namespace hpnApp {

use \hpnWse\stNumUtil;
use \hpnWse\stStrUtil;
use \hpnWse\stObjUtil;
use \hpnWse\stAryUtil;
use \hpnWse\stHttpSvc;
use \hpnWse\stSqlUtil;
use \hpnWse\nWp\stBase as stWpBase;

/// 动作锁
function fActnLock($a_Lmt, $a_Uid)
{
	$l_Pdo = stHttpSvc::cCnctToDb();
	return stSqlUtil::cActnLock($l_Pdo, (DB_NAME . '.t_user'), 'mActionLock', $a_Lmt,
		'mId', $a_Uid);
}

/// 验证用户，返回null表示没有错误
function fVldtUser($a_Lmt)
{
	if (!fIsMember())
	{
		return stHttpSvc::cRspsErr(401, -2, '未登录！');
	}
	if (($a_Lmt > 0) && !fActnLock($a_Lmt, $_SESSION['UserProfile']['Id']))
	{
		return stHttpSvc::cRspsErr(403, -2, '操作过于频繁！');
	}
	return null;
}

/// 为问题补充默认值
function fSuplDftsForQstns(&$a_Qstns, $a_Len = 160)
{
	foreach ($a_Qstns as $l_Idx => $l_Qstn)
	{
		fSuplDftsForQstn($a_Qstns[$l_Idx], $a_Len);
	}
}

function fSuplDftsForQstn(&$a_Qstn, $a_Len)
{
//	$a_Qstn['guid'] = site_url() . '/?p=' . $a_Qstn['ID'] ;
	$a_Qstn['guid'] = site_url() . '/' . $a_Qstn['ID'] . '.html';

	$l_Excp = $a_Qstn['post_content'];
	if (stStrUtil::cGetLen($l_Excp) > $a_Len)
	{
		$l_Excp = stStrUtil::cSub($l_Excp, 0, $a_Len - 4);
		$l_Excp .= ' ...';
	}

	$a_Qstn['post_excerpt'] = $l_Excp;

	if (!\hpnWse\fBool($a_Qstn['Athr']))
	{
		$a_Qstn['Athr'] = '医问便知网';
		$a_Qstn['Ava'] = (site_url() . '/img/Logo.png');
	}

	if (!\hpnWse\fBool($a_Qstn['Ava']))
	{
		$a_Qstn['Ava'] = (site_url() . '/img/DftAvatar.png');
	}

	if (!\hpnWse\fBool($a_Qstn['Imgs']))
	{
		$a_Qstn['Imgs'] = ';;';
	}
	$a_Qstn['Imgs'] = explode(';', $a_Qstn['Imgs']);
	if (!$a_Qstn['Imgs'][0])
	{
		$a_Qstn['Imgs'][0] = (site_url() . '/img/DftQstnImg.jpg');
	}
}

/// 获取问题元数据
function fFchQstnMeta($a_Qid)
{
	$l_Db = DB_NAME;

$l_Sql = <<<SQL
SELECT mImages AS Imgs, mPositive AS Pstv, mNegative AS Ngtv, mFavorite AS Fav
FROM $l_Db.t_question_meta
WHERE (mQuestionId = $a_Qid)
SQL;

	$l_Pdo = stHttpSvc::cCnctToDb();
	$l_Rst = stSqlUtil::cPrprExecFchOne($l_Pdo, $l_Sql);

	stSqlUtil::cIntCols('Pstv,Ngtv,Fav', $l_Rst); // 转成整数

	$l_Rst['Imgs'] = explode(';', $l_Rst['Imgs']);
	for ($i=count($l_Rst['Imgs']) - 1; $i>=0; --$i) // 去除空白图像
	{
		if (!$l_Rst['Imgs'][$i])
		{ stAryUtil::cErs($l_Rst['Imgs'], $i); }
	}
	
	return $l_Rst;
}

/// 获取用户对给定问题的评价，-1=踩；0=未评价；+1=赞
function fFchUserAprsForQstn($a_Uid, $a_Qid)
{
	$l_Db = DB_NAME;

$l_Sql = <<<SQL
SELECT mAppraise AS Aprs
FROM $l_Db.tt_user__question__appraise AS tA
WHERE (tA.mUserId = $a_Uid) AND (tA.mQuestionId = $a_Qid)
SQL;

	$l_Pdo = stHttpSvc::cCnctToDb();
	$l_Rst = stSqlUtil::cPrprExecFchNum($l_Pdo, $l_Sql);
	return (null === $l_Rst) ? 0 : $l_Rst;
}

/// 获取用户对给定问题的喜爱（收藏），Boolean
function fFchUserLikeForQstn($a_Uid, $a_Qid)
{
	$l_Db = DB_NAME;

$l_Sql = <<<SQL
SELECT COUNT(*)
FROM $l_Db.tt_user__question__favorite AS tF
WHERE (tF.mUserId = $a_Uid) AND (tF.mQuestionId = $a_Qid)
SQL;

	$l_Pdo = stHttpSvc::cCnctToDb();
	$l_Rst = stSqlUtil::cPrprExecFchNum($l_Pdo, $l_Sql);
	return (1 === $l_Rst);
}

} // namespace hpnApp

//////////////////////////////////// OVER ////////////////////////////////////